﻿using System;
using System.Data;
using System.Configuration;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using DragDraw.Service;

namespace DragDraw.Service
{
    [TestFixture]//注明这个类是单元测试类，这样NUnit测试工具可以找到这个类
    public class Test
    {
        public static void output(List<string> friend)
        {
            Console.WriteLine("FriendRelation");
            foreach (string str in friend)
                Console.WriteLine(str);
        }
        public static void output(bool tem)
        {
            if (tem == true)
                Console.WriteLine("Register success");
            else Console.WriteLine("Register fail");
        }
        public static void output(SpaceInfo spaceinfo)
        {
            if (spaceinfo != null)
            {
                Console.WriteLine("SpaceID: {0} \t OwnerUserID: {1} \n BackgroundImage:{2} \t ThemeColor:{3}", spaceinfo.SpaceID, spaceinfo.OwnerUserID, spaceinfo.BackgroundImage, spaceinfo.ThemeColor);
            }
            else Console.WriteLine("The space is not existent");
        }
        public static void output(UserInfo user)
        {
            if (user != null)
                Console.WriteLine("userID:{0}\nRealname{1}", user.UserID, user.Realname);
            else Console.WriteLine("User is not existent!");
        }
        public static void output(List<WidgetInfo> widget)
        {
            if (widget == null)
            {
                Console.WriteLine("Widget is empty");
                return;
            }
            else Console.WriteLine("Widget:\n");
            foreach (WidgetInfo tem in widget)
            {
                Console.WriteLine("spacdID:{0}\nPosX:{1}\nHeight:{2}\n\n", tem.OwnerSpaceID, tem.PosX, tem.Height);
            }
        }
        public static void output(WidgetInfo widget)
        {
            Console.WriteLine("widgetID: {0}   OwnerSpaceID: {1}\nLibInfoID: {2}\nPox:({3},{4})   Size: {5}X{6}",
                widget.WidgetID, widget.OwnerSpaceID, widget.WidgetLibInfoID, widget.PosX, widget.PosY, widget.Width, widget.Height);
        }
        public static void output(List<WidgetLibInfo> libList)
        {
            if (libList == null)
                Console.WriteLine("LibIsEmpty");
            foreach (WidgetLibInfo lib in libList)
            {
                Console.WriteLine("Name:{0}\nFileName:{1}\nAssembly:{2}\nTags:{3}\nImageUrl:{4}\nDescription:{5}",
                    lib.WidgetName, lib.WidgetFileName, lib.WidgetAssemblyName, lib.Tags, lib.ImageUrl, lib.Description);
            }
        }
        public static void output(List<SysMessage> msgList)
        {
            foreach (SysMessage msg in msgList)
            {
                Console.WriteLine("\nTag:{0}  Body:{1}\nIsHandle:{2}\n\n", msg.Tag, msg.Body, msg.IsHandled.ToString());
            }
        }
        [SetUp]//注明这是测试初始化函数，每一个测试运行前都会运行这个函数
        public void Init()
        {
           
        }
        [Test]
        public void GetUserTest()
        {
            //Register();
            output(MyService.service.GetUserInfo("4453"));
            Console.WriteLine(MyService.service.GetLastEror());
            Console.WriteLine(MyService.service.GetLastEror());
            Console.WriteLine(MyService.service.GetLastEror());
            Console.WriteLine(MyService.service.GetLastEror());
            Console.WriteLine(MyService.service.GetLastEror());
        }
        // 测试通过
        [Test]
        public void RegisterTest()
        {
            UserInfo user = new UserInfo("chen", "sheng");
            string password = "13579";
            Assert.AreEqual(true, MyService.service.Register(user, password));
            Assert.AreEqual(true, UserInfoData.Delete(user.UserID));
            UserInfo user2 = new UserInfo("","");
            Assert.AreEqual(false, MyService.service.Register(user2, password));
            Assert.AreEqual(false, MyService.service.Register(user, ""));
        }
        // 测试通过
        [Test]
        public void LoginTest()
        {
            string userID = "445363200";
            string password = "12345678";
            string realname = "sheng";
            UserInfo user = new UserInfo(userID, realname);
            Assert.AreEqual(true, MyService.service.Register(user, password));
            bool isLogin = MyService.service.Login(userID, password);
            Assert.AreNotEqual(false, isLogin);
            Assert.AreEqual(null, MyService.service.Login(userID + "23", password));
            Assert.AreEqual(null, MyService.service.Login(userID, password + "1"));
            Assert.AreEqual(true, UserInfoData.Delete(userID));
        }
        public void Register()
        {
            string userID = "2008";
            string password = "12345678";
            string realname = "sheng";
            UserInfo user = new UserInfo(userID, realname);
            MyService.service.Register(user, password);
        }
        public void Del()
        {
            string userID = "2008";
            MyService.service.DeleteUserInfo(userID);
        }
        // 测试通过
        [Test]
        public void SetGetUserInfoTest()
        {
            Register();
            string userID = "2008";
            UserInfo user = MyService.service.GetUserInfo(userID);
            user.Realname = "Happy";
            Assert.AreEqual(true, MyService.service.SetUserInfo(user));
            output(MyService.service.GetUserInfo(userID));
            Assert.AreEqual(true,MyService.service.SetPassword(user.UserID, "today"));
            user.UserID = userID + "30";
            Assert.AreEqual(false, MyService.service.SetPassword(user.UserID, "tomorrow"));
            user.UserID = userID;
            Console.WriteLine(UserAccountData.Query(userID));
            Del(); 
        }
        [Test]
        public void SpaceTest()
        {
            Register();
            string userID = "2008";
            SpaceInfo space;
            Assert.AreNotEqual(null, space = MyService.service.GetSpaceInfo(userID));
            output(space);            
            space.BackgroundImage = "http://baidu.com";
            int  spaceID = space.SpaceID;
            space.ThemeColor = "ABCDEF";
            Assert.AreEqual(true, MyService.service.SetSpaceInfo(space));
            space.OwnerUserID = "3379";
            Assert.AreEqual(false, MyService.service.SetSpaceInfo(space));
            space.OwnerUserID = userID;
            space.SpaceID = 999;
            Assert.AreEqual(false, MyService.service.SetSpaceInfo(space));
            Del();
        }
        // 测试通过
        [Test]
        public void WidgetTest()
        {
            Register();
            string userID = "2008";
            SpaceInfo space;
            Assert.AreNotEqual(null, space = SpaceInfoData.Query(userID));

            WidgetInfo widget = new WidgetInfo();
            widget.OwnerSpaceID = 5;

            WidgetLibInfo lib = new WidgetLibInfo();
            lib.ImageUrl = "http:\\local.mycomput";
            lib.Tags = "Common";
            lib.WidgetFileName = "Chat";
            lib.WidgetAssemblyName = "photoo";
            lib.WidgetName = "myOwnWidget";
            lib.Description = "This is the lib built by client";
            lib.ID = WidgetLibInfoData.Insert(lib);
            List<WidgetLibInfo> libList = WidgetLibInfoData.Query();
            WidgetLibInfo myLib = libList[0];

            Assert.AreEqual(-1, MyService.service.AddWidgetInfo(widget));
            widget.WidgetLibInfoID = 20;
            Assert.AreNotEqual(-1,widget.WidgetID= MyService.service.AddWidgetInfo(widget));
            Assert.AreEqual(false, MyService.service.DeleteWidgetInfo(20, widget.WidgetID));
            
            widget.PosX = 200;
            widget.Height = 300;
            Assert.AreEqual(true, MyService.service.UpdateWidgetInfo(widget));
            //Assert.AreEqual(true, WidgetInfoData.Update(widget));
            Assert.AreEqual(true, MyService.service.DeleteWidgetInfo(widget.OwnerSpaceID, widget.WidgetID));
            output(libList);
            foreach (WidgetLibInfo tem in libList)
            {
                WidgetLibInfoData.Delete(tem.ID);
            }
            output(WidgetLibInfoData.Query());
            Del();
        }
        public void doubleRegister()
        {
            string userID1 = "3933", userID2 = "5744";
            string realname1 = "chen", realname2 = "sheng";
            string pass1 = "1234", pass2 = "4567";
            UserInfo user1 = new UserInfo(userID1, realname1);
            UserInfo user2 = new UserInfo(userID2, realname2);
            MyService.service.Register(user1, pass1);
            MyService.service.Register(user2, pass2);
        }
        public void doubleDel()
        {
            string userID1 = "3933", userID2 = "5744";
            MyService.service.DeleteUserInfo(userID1);
            MyService.service.DeleteUserInfo(userID2);       
        }
        [Test]
        public void MessageTest()
        {
            doubleRegister();
            string userID1 = "3933", userID2 = "5744";

            //Assert.AreEqual(true,MyService.service.SendMessage(userID1, userID1, "common", "you are"));
            List<SysMessage> MsgList = SysMessageData.QueryList(userID2);
            List<SysMessage> msgList2 = SysMessageData.QueryList(userID2);
            MsgList[0].Body = "hahahahahahahah";
            output(MsgList);
            output(msgList2);
            SysMessage msg = new SysMessage();
            msg.SourceID = userID1;
            msg.DestinationID = userID2;
            msg.Tag = "good";
            msg.IsHandled = false;
            msg.Body = "very good";            
            //Assert.AreEqual(true,SysMessageData.Insert(msg));
            doubleDel();
        }
    }
    public class MyService
    {
        static public UserService service;
        static MyService()
        {
            service = new UserService();
        }
    }
}
